package com.gdu.video;

import java.io.File;

import android.app.Activity;
import android.app.ActionBar;
import android.app.ActivityManager;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ConfigurationInfo;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.Time;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity implements OnClickListener{

	RealVideo mRealVideo;
	GLFrameSurface mGLSurfaceView;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);

		Button button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(this);
		
		Button activityBtn=(Button)findViewById(R.id.btn_activity);
		activityBtn.setOnClickListener(this);
		
	 mGLSurfaceView = (GLFrameSurface) findViewById(R.id.surfaceView1);
		// Check if the system supports OpenGL ES 2.0.
		final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		final ConfigurationInfo configurationInfo = activityManager
				.getDeviceConfigurationInfo();
		final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
		if (supportsEs2) {
			// Request an OpenGL ES 2.0 compatible context.
			mGLSurfaceView.setEGLContextClientVersion(2);

			DisplayMetrics dm = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(dm);
//			GLFrameRenderer render = new GLFrameRenderer(this, mGLSurfaceView);
			WFRender render=new WFRender(mGLSurfaceView);
			render.setShowRect(720, 576);
			mGLSurfaceView.setRenderer(render);
			
			mRealVideo = new RealVideo(render, null);
		}
    }

    @Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view.getId() == R.id.button1) {
			mRealVideo.OpenStream("/storage/emulated/0/ProFlight/Video/ProdroneVideo1458291384707.mp4");
//			mRealvideo.OpenStream("rtsp://192.168.11.10/0", 0);
//			mRealvideo.OpenStream("rtsp://192.168.11.123/1/mjpeg", 0);
//			mRealvideo.OpenStream("rtsp://192.168.1.111:554/user=admin_password=tlJwpbo6_channel=1_stream=0.sdp?real_stream", 0); //rtsp://192.168.42.1:8554/video //"rtsp://192.168.1.168:8557/PSIA/Streaming/channels/2?videoCodecType=H.264"
		} else if (view.getId() == R.id.button2) {
			mRealVideo.CloseStream();
		}else if(view.getId()==R.id.btn_activity){
			mGLSurfaceView.setVisibility(View.GONE);
			mRealVideo.changeFilter();
			mGLSurfaceView.setVisibility(View.VISIBLE);
	
		}
			
		
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
	static {
		
//		System.loadLibrary("avutil-54");
//		System.loadLibrary("swresample-1");
//		System.loadLibrary("avcodec-56");
//		System.loadLibrary("avformat-56");
//		System.loadLibrary("swscale-3");
		System.loadLibrary("Video");
	}

}
